<?php
/**
 * User
 *
 * PHP version 5
 *
 * @category Controller
 * @version  1.0
 * @author   Duonght <hduongdmit@gmail.com>
 */
App::uses('AppController', 'Controller');
class SupportController extends AppController {

	public $uses = array('Support');
	/**
	 * Components
	 * @var array
	 */
	public $components = array('Paginator');

	public function index(){
		$conditions = array('del_flg' => 0);
		$paginate = array(
				'fields' => '*',
				'limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
				'conditions' => $conditions,
				'order' => array(
						'order' => 'ASC',
				),
		);

		$this->Paginator->settings = $paginate;
		$listSupport = $this->Paginator->paginate('Support');
		//force view
		$this->set(array(
				'datas' => $listSupport,
				'page_number_limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
				'title_for_layout' => $this->moduleAdmin['support']['index'],
		));

	}

	/**
	 * Edit news
	 *
	 * @param string $id
	 */
	public function edit($id = null){
		if (!$id) {
			throw new NotFoundException(__('Invalid post'));
		}

		//get detail
		$detail = $this->Support->findById($id);
		if (!$detail) {
			throw new NotFoundException(__('Invalid post'));
		}

		//edit news
		if ($this->request->is('post')) {
			if ($this->Support->save($this->request->data)) {
				$this->Session->setFlash(__('Updated success.'), 'success');
				return $this->redirect(array('action' => 'index'));
			}

		}

		//set view
		$this->set('detail', $detail);
		$this->set('title_for_layout', $this->moduleAdmin['support']['edit']);
	}

	public function add(){
		if ($this->request->is('post')) {
			$this->Support->create();
			if ($this->Support->save($this->request->data)) {
				$this->Session->setFlash(__('Add support Success'), 'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Save could not be saved. Please, try again'), 'error');
			}
		}

		//forge view
		$this->set('title_for_layout', $this->moduleAdmin['support']['add']);
	}

	/**
	 * Delete banner
	 *
	 * @param string $id
	 */
	public function delete($id = null){
		$this->layout = null;
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}

		$this->Support->read('id', $id);
		$this->Support->set('del_flg', 1);
		$this->Support->save();
		$this->Session->setFlash(__('Delete success'), 'success');
		return $this->redirect(array('action' => 'index'));

	}

	/**
	 * Update banner active
	 * @param string $id
	 * @param string $type
	 */
	public function update($id = null, $field = 'active'){
		$this->layout = null;
		$this->render(false);
		if ($this->request->is('post')) {
			$value = $this->request->data['value'];
			$this->Support->read('id', $id);
			$this->Support->set($field, $value);
			$this->Support->save();
			echo $value;
		}
	}

}
